'  ==========================================================================
'  |                                                                        |
'  | SQLitening Server Monitor                                              |
'  |                                                                        |
'  ==========================================================================
'  If SQLitening Server is compiled with %CompileStartServerMonitor = %True
'  then it will shell to this process when it starts. This process will run 
'  as long as the server is running, it will exit when the server is  stopped.  
'  This is the place you can code things like back up or do any other database 
'  maintenance work using all the power of SQLitening in local mode.  If this 
'  monitor is not required then just compile SQLitening Server with 
' %CompileStartServerMonitor = %False.
'  ==========================================================================
'  PUBLIC DOMAIN SOFTWARE
'  The author or authors of this code dedicate any and all
'  copyright interest in this code to the public domain.
'  Anyone is free to copy, modify, publish, use, compile,
'  sell, or distribute the original code, either in source
'  code form or as a compiled binary, for any purpose,
'  commercial or non-commercial, and by any means.
'  Fred Meier - July 2010
'  ==========================================================================

#Compile Exe "..\Bin\SQLiteningServerMonitor.Exe"
#Dim All
#Include "..\Inc\SQLitening.Inc"

'============================<[ Equates ]>=============================
' ==========>>> SQLite
%SQLITE3_OK     =   0   '*/ Successful result */
%SQLITE3_ROW    = 100   '*/ sqlite3_step() has another row ready */
%SQLITE3_DONE   = 101   '*/ sqlite3_step() has finished executing */
%SQLITE_OPEN_READONLY = &H00000001
%SQLITE_OPEN_CREATE = &H00000004

' ==========>>> Misc
%True = -1
%False = 0
%kSynchonize = &H100000
%kWaitObject = 0
%kWaitTimeout = &H102
%kWaitFailed = -1
%kWaitTime = 100000           ' Number of miliseconds to sleep between each monitor -- 10000 = 10 seconds

'============================<[ Declares ]>============================
Declare Function OpenProcess LIB "KERNEL32.DLL" ALIAS "OpenProcess" (BYVAL dwDesiredAccess AS DWORD, BYVAL bInheritHandle AS LONG, BYVAL dwProcessId AS DWORD) AS LONG
Declare Function WaitForSingleObject LIB "KERNEL32.DLL" ALIAS "WaitForSingleObject" (BYVAL hHandle AS LONG, BYVAL dwMilliseconds AS LONG) AS LONG

'=============================<[ Global ]>=============================

'===========================<[ Shut Down ]>============================
Sub ShutDown
'  This is called one time when the server stops.  Do any clean up work here.
   
End Sub

'============================<[ Monitor ]>=============================
Sub Monitor
'  This is called every n seconds depending on %kWaitTime.  Do your work here.

End Sub

'==============================<[ Main ]>==============================
Function PbMain()

   Local lhProcessHand As Dword

   ' Set current folder
   chdir  exe.path$

   ' Wait for server to end
   lhProcessHand = OpenProcess(%kSynchonize, %False, val(command$))
   Do
      Select Case WaitForSingleObject(lhProcessHand, %kWaitTime)
      case %kWaitTimeout    'the timer expired on wait so call Monitor
         Monitor
      Case %kWaitObject, %kWaitFailed     'if lkWaitObject then it completed
                                          'if lkWaitFailed then it completed before we opened process
         ShutDown
         exit do
      end select
   Loop

End Function

